package com.cmccgz.udacs_landlord_webapp.main.housemanager.list;

import com.cmccgz.udacs_landlord_webapp.bean.HouseListDataBean;
import com.cmccgz.udacs_landlord_webapp.data.HouseManagerDataSource;
import com.cmccgz.udacs_landlord_webapp.data.HouseManagerRepository;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/5 09:11
 */

public class HouseListPresenter implements HouseListContract.HouseListPresenter {

    private final HouseListContract.HouseListView mListView;
    private HouseManagerRepository mRepository;
    private String searchType;

    public HouseListPresenter(HouseManagerRepository houseManagerRepository, HouseListContract.HouseListView listFragment) {
        this.mRepository = houseManagerRepository;
        this.mListView = listFragment;
        this.mListView.setPresenter(this);
    }

    @Override
    public void setSearchType(String type) {
        searchType = type;
    }

    @Override
    public void getData(String keyword, String oCond, final int page) {
        if (!mListView.isActive()) {
            return;
        }
        Logger.i("关键字：" + keyword + "-----页码：" + page);
        mRepository.getHouseList(searchType, keyword, oCond, page, new HouseManagerDataSource.HouseListCallback() {
            @Override
            public void onHouseListLoaded(List<HouseListDataBean> list) {
                if (page == 1) {
                    mListView.refreshDataSuccess(list);
                } else {
                    mListView.loadMoreDataSuccess(list);
                }
            }

            @Override
            public void onLoadedFailed(String msg) {
                if (page == 1) {
                    mListView.refreshDataFailed(msg);
                } else {
                    mListView.loadMoreDataFailed(msg);
                }
            }
        });
    }

    @Override
    public void start() {
        mListView.showLoading();
    }

}
